package com.zhuhjay.common.swagger.config.plugins;

import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Operation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.Collections;
import java.util.List;

import static com.zhuhjay.common.base.constant.ApplicationConstant.LOGOUT_REQUEST_MATCHER;
import static com.zhuhjay.common.base.constant.ApplicationConstant.LOGOUT_REQUEST_METHOD;

/**
 * 手动添加登出接口
 * @author ZhuHJay
 * @date 2023/2/27 10:19
 */
@Component
public class LogoutApiScannerPlugin implements ApiListingScannerPlugin {

    /** 实现此方法可手动添加ApiDescriptions */
    @Override
    public List<ApiDescription> apply(DocumentationContext context) {
        Operation usernamePasswordOperation = new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.valueOf(LOGOUT_REQUEST_METHOD))
                .summary("用户登出")
                .notes("用户登出清理数据")
                .tags(Collections.singleton("公共接口"))
                .uniqueId("logout")
                .consumes(Collections.singleton(MediaType.APPLICATION_JSON_VALUE))
                .produces(Collections.singleton(MediaType.APPLICATION_JSON_VALUE))
                .build();

        ApiDescription loginApiDescription = new ApiDescription(
                null, LOGOUT_REQUEST_MATCHER,
                "登出接口", Collections.singletonList(usernamePasswordOperation), false);

        return Collections.singletonList(loginApiDescription);
    }

    /**
     * 是否使用此插件
     * @param documentationType swagger文档类型
     * @return true 启用
     */
    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
